import Vue from 'vue'
import Vuex from 'vuex'
import { login } from '../http/api'

Vue.use(Vuex) //vue的插件机制
//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		userInfo: null || uni.getStorageSync('userInfo'), // * 用户信息
		sessionKey: null || uni.getStorageSync('sessionKey'), // * 加密密匙
		token: null || uni.getStorageSync('token'), // * token
		userId: null || uni.getStorageSync('userId'), // * 用户id
		curHos: null || uni.getStorageSync('curHos'), // * 当前医院名称
		orgId: null || uni.getStorageSync('orgId'), // * 当前机构id
		orgCode: null || uni.getStorageSync('orgCode'), // * 当前机构code
		CardId: null || uni.getStorageSync('CardId'), // * 当前就诊卡
		patientId: null || uni.getStorageSync('patientId'), // * 当前病人id
		patientName: null || uni.getStorageSync('patientName'), // * 当前病人名称
		patientIdentity: null || uni.getStorageSync('patientIdentity'), // * 当前病人身份证号
		patientTelephone: null || uni.getStorageSync('patientTelephone'), // * 当前病人手机号
		patientcardNo: null || uni.getStorageSync('patientcardNo'), // * 当前病人卡号
		patientGender: null || uni.getStorageSync('patientGender'), // * 当前病人性别
	},
	mutations: {
		getUserInfo(state) {
			if (state.token && state.sessionKey && state.userId) {
				console.log('用户已登录')
			} else {
				uni.showModal({
					title: '温馨提示',
					content: '亲，授权微信登录后才能正常使用小程序功能',
					success(res) {
						if (res.confirm) {
							uni.getUserProfile({
								desc: '登录',
								lang: 'zh_CN',
								success: res => {
									uni.login({
										provider: 'weixin',
										success: function (loginRes) {
											login({ code: loginRes.code }).then(({ data, head }) => {
												if (head.code === 200) {
													state.token = data.token
													state.sessionKey = data.sessionKey
													let user = data.user
													state.userId = user.id
													uni.setStorageSync('sessionKey', data.sessionKey)
													uni.setStorageSync('token', data.token)
													uni.setStorageSync('userId', user.id)
													// console.log(state, 222);
													user.userNickName = res.userInfo.nickName
													user.imageUrl = res.userInfo.avatarUrl
													state.userInfo = user
													uni.setStorageSync('userInfo', user)
													if (
														user.userIdcode &&
														user.userName &&
														user.userGender
													) {
														uni.reLaunch({
															url: '/pages/index/index',
														})
													} else {
														uni.navigateTo({
															url: '/pages/addUserInfo/index',
														})
													}
												}
											})
										},
									})
								},
							})
						}
					},
				})
			}
			// }
		},
		changeUserInfo(state, data) {
			let { sessionKey, token, user } = data
			state.sessionKey = sessionKey
			state.token = token
			state.userId = user.id
		},
		updateUser(state, info) {
			state.userInfo = info
			uni.setStorageSync('userInfo', info)
		},
		patientIdcode(state, info) {
			state.patientId = info
			uni.setStorageSync('patientId', info)
		},
		patientCardId(state, info) {
			state.CardId = info
			uni.setStorageSync('CardId', info)
		},
		patientName(state, info) {
			state.patientName = info
			uni.setStorageSync('patientName', info)
		},
		patientIdentity(state, info) {
			state.patientIdentity = info
			uni.setStorageSync('patientIdentity', info)
		},
		patientTelephone(state, info) {
			state.patientTelephone = info
			uni.setStorageSync('patientTelephone', info)
		},
		patientcardNo(state, info) {
			state.patientcardNo = info
			uni.setStorageSync('patientcardNo', info)
		},
		patientGender(state, info) {
			state.patientGender = info
			uni.setStorageSync('patientGender', info)
		},
		changeCurHos(state, curHos) {
			state.curHos = curHos.name
			state.orgId = curHos.id
			state.orgCode = curHos.orgCode
			uni.setStorageSync('curHos', curHos.name)
			uni.setStorageSync('orgId', curHos.id)
			uni.setStorageSync('orgCode', curHos.orgCode)
		},
	},
})
export default store
